import 'package:get/get.dart';
import 'package:zxcvbn/zxcvbn.dart';

import 'common_utils/log_util.dart';

class CheckPassword {
  static (double, String) getWarningMessage(String password) {
    double score = 0;
    String warning = "";

    final zxcvbn = Zxcvbn();
    final result = zxcvbn.evaluate(password);
    score = result.score ?? 0;
    warning = result.feedback.warning ?? "";
    LogUtil.e("密码：$password");
    LogUtil.e("得分：$score");
    LogUtil.e("Feedback: ${result.feedback.warning}");

    // 根据warning信息返回对应的多语言key
    String translatedWarning = "";
    switch (warning) {
      case "Straight rows of keys are easy to guess":
        translatedWarning = "password_warning_straight_keys".tr;
        break;
      case "Short keyboard patterns are easy to guess":
        translatedWarning = "password_warning_short_pattern".tr;
        break;
      case "Repeats like 'aaa' are easy to guess":
        translatedWarning = "password_warning_repeat_char".tr;
        break;
      case "Repeats like 'abcabc' are only slightly harder to guess than 'abc'":
        translatedWarning = "password_warning_repeat_pattern".tr;
        break;
      case "Sequences like abc or 6543 are easy to guess":
        translatedWarning = "password_warning_sequence".tr;
        break;
      case "Recent years are easy to guess":
        translatedWarning = "password_warning_recent_year".tr;
        break;
      case "Dates are often easy to guess":
        translatedWarning = "password_warning_date".tr;
        break;
      case "This is a top-10 common password":
        translatedWarning = "password_warning_top_10".tr;
        break;
      case "This is a top-100 common password":
        translatedWarning = "password_warning_top_100".tr;
        break;
      case "This is a very common password":
        translatedWarning = "password_warning_very_common".tr;
        break;
      case "This is similar to a commonly used password":
        translatedWarning = "password_warning_similar".tr;
        break;
      case "A word by itself is easy to guess":
        translatedWarning = "password_warning_single_word".tr;
        break;
      case "Names and surnames by themselves are easy to guess":
        translatedWarning = "password_warning_name".tr;
        break;
      case "Common names and surnames are easy to guess":
        translatedWarning = "password_warning_common_name".tr;
        break;
      case "Capitalization doesn't help very much":
        translatedWarning = "password_warning_capitalization".tr;
        break;
      case "All-uppercase is almost as easy to guess as all-lowercase":
        translatedWarning = "password_warning_all_upper".tr;
        break;
      case "Reversed words aren't much harder to guess":
        translatedWarning = "password_warning_reversed".tr;
        break;
      case "Predictable substitutions like '@' instead of 'a' don't help very much":
        translatedWarning = "password_warning_l33t".tr;
        break;
      default:
        translatedWarning = "";
    }

    return (score, translatedWarning);
  }

  static List<String> getSuggestions(String password) {
    final zxcvbn = Zxcvbn();
    final result = zxcvbn.evaluate(password);
    return result.feedback.suggestions ?? [];
  }
}
